package cn.itcast.dao;

import cn.itcast.pojo.Menu;
import cn.itcast.vo.MenuListVo;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MenuDao {
    Integer save(Menu menu);

    Integer delete(Integer id);

    Integer deleteByParentMenuId(Integer menuId);

    Integer edit(Menu menu);

    Menu findByid(Integer id);

    List<Menu> findAll();

    Menu findByName(String name);

    Integer saveMenuAndPermission(@Param("menuId") Integer menuId, @Param("permissionId") Integer permissionId);

    List<Integer> findPermissionIdsByMenuId(Integer menuId);

    void deleteMenuAndPermission(Integer menuId);

    List<Menu> findByParentMenuId(Integer id);

    @Select("select * from t_menu where level=1 order by priority asc")
    @Results({
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "linkUrl",property = "linkUrl"),
            @Result(column = "priority",property = "priority"),
            @Result(column = "icon",property = "icon"),
            @Result(column = "description",property = "description"),
            @Result(column = "parentMenuId",property = "parentMenuId"),
            @Result(column = "level",property = "level"),
            @Result(
                    property = "children",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "cn.itcast.dao.MenuDao.findByParentMenuId")
            )
    })
    Page<Menu> findParentMenu();



    @Select("SELECT DISTINCT * FROM t_menu m,t_user u,t_user_role ur,t_role r,t_role_menu rm WHERE u.id=ur.user_id AND ur.role_id=r.id AND r.id=rm.role_id AND rm.menu_id=m.id AND m.`level`=1 AND u.id=#{userId} order by priority asc")
    @Results({
            @Result(column = "name",property = "title"),
            @Result(column = "linkUrl",property = "linkUrl"),
            @Result(column = "icon",property = "icon"),
            @Result(
                    property = "children",
                    javaType = List.class,
                    column = "id",
                    many = @Many(select = "cn.itcast.dao.MenuDao.findMenuVoByParentMenu")
            )
    })
    List<MenuListVo> findMenuByUserId(Integer userId);

    @Select("SELECT * FROM t_menu where parentMenuId=#{id} order by priority asc")
    @Results({
            @Result(column = "name",property = "title"),
            @Result(column = "linkUrl",property = "linkUrl"),
            @Result(column = "icon",property = "icon")
    })
    List<MenuListVo> findMenuVoByParentMenu(Integer Id);

    void deleteMenuAndRole(Integer menuId);

    List<String> findMenuNameByUserId(Integer userId);

    Integer findRoleIdByMenuId(Integer menuId);
}
